home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / optivc32 / fitdemo.cpp < prev    next >
C/C++ Source or Header  |  1999-03-06  |  25KB  |  481 lines

  1. /***********************   FITDEMO.CPP  **********************************
  2. *                                                                        *
  3. *            Data-fitting demo program for                               *
  4. *                  O p t i V e c                                         *
  5. *          with Borland C++ 4.x, 5.x, C++ Builder,                       *
  6. *           or Microsoft Visual C++ 5.0 or 6.0                           *
  7. *                                                                        *
  8. *       Copyright 1996-1999 by Martin Sander                             *
  9. *                                                                        *
  10. *                                                                        *
  11. *       This sample program is meant to provide you with some basic      *
  12. *       examples of code for the use of OptiVec's data-fitting routines. *
  13. *       Especially for the non-linear and multi-experiment functions,    *
  14. *       the user will have to adapt this code to his specific problem.   *
  15. *                                                                        *
  16. **************************************************************************/
  17. /*
  18. Borland C++, Command-line:
  19.     a) 32-bit: type
  20.                BCC32 -W fitdemo.cpp vcf3w.lib
  21.     b) 16-bit: type
  22.                BCC -ml -W fitdemo.cpp vcl3w.lib mcl3w.lib
  23. Borland C++, IDE:
  24.     1. Open the new-project menu with  Project/New.
  25.     2. Create a project FITDEMO in the OptiVec directory,
  26.        e.g., \bc\optivec.
  27.     3.a) 32-bit: Choose Application[EXE] for Win32 GUI, static linking,
  28.                  single-thread.
  29.     3.b) 16-bit: Choose Application[EXE] for Windows3.x, memory model LARGE.
  30.     4. Hit OK.
  31.     5. In the Project window that will now be on the screen, delete the
  32.        nodes FITDEMO.DEF and FITDEMO.RC.
  33.     6.a) 32-bit: Add the node VCF3W.LIB.
  34.     6.b) 16-bit: Add the nodes VCL3W.LIB and MCL3W.LIB.
  35.     7. In Options/Project, be sure the include-file search path and the
  36.        library search path both include the respective  OPTIVEC directories,
  37.        e.g.: \bc\optivec\include and bc\optivec\lib, resprectively
  38.     8. Compile and run.
  39. Microsoft Visual C++:
  40.     1. Create a new project as a "Win32 application".
  41.     2. In the project settings, C/C++, Code Generation,
  42.        verify that single-thread debug is chosen.
  43.     3. Add \OptiVec\include to the include-file search path.
  44.     4. Add the files  FITDEMO.CPP  and  OVVCSD.LIB to your project.
  45.     5. Compile and run.
  46. */
  47.  
  48. #include <windows.h>                    /* Compiler's include files */
  49. #include <stdio.h>
  50. #include <math.h>
  51. #include <string.h>
  52.  
  53. #include <VDstd.h>                      /* OptiVec include files */
  54. #include <VDmath.h>
  55. #include <MDstd.h>
  56. #include <VIstd.h>
  57. #include <Vgraph.h>
  58. HWND     hWndMain;
  59. int      vview;
  60. dVector  XExp, X2, YExp, YFit, YExp2, YFit2, YExp3, YFit3;
  61. ui       sizex;
  62. double   FitPars[7];   // the highest number of parameters we will have in our examples
  63. int      ParStatus[7];
  64. #define  polydeg 5     // refers to the polynomial fitting example
  65. #ifndef M_PI
  66.     #define M_PI  3.14159265358979323846
  67. #endif
  68. NEWMATHERR  // this macro has an effect only for 16-bit BC
  69.  
  70. LONG FAR PASCAL MainMessageHandler (HWND, UINT, WPARAM, LPARAM);
  71.  
  72.     // the following function is used with VD_linfit:
  73. static void PolyModel( dVector BasFuncs, double x, unsigned  nfuncs )
  74. {  /* This function fills the vector BasFuncs with powers of x.
  75.       VD_linfit will then determine the coefficient for each of these
  76.       powers.
  77.       Note that the coefficients do not occur in the model function!
  78.       The basis functions with known coefficients (whose fitting has
  79.       been disabled) are still part of the model and must be calculated.
  80.       You will see below that the values of the known (disabled)
  81.       coefficients  must be set prior to calling VD_linfit.          */
  82.      BasFuncs[0] = 1.0;
  83.      for( unsigned i=1; i<nfuncs; i++ )
  84.           BasFuncs[i] = BasFuncs[i-1]*x;
  85. }
  86.  
  87.    // the following function is used with VD_nonlinfit:
  88. static void VPolyModel( dVector Y, dVector X, ui size )
  89. {   /* Here, the model function has to fill a whole result vector,
  90.        using your first guess of FitPars. In contrast to the
  91.        linear case, now the coefficients are explicitly used in the
  92.        model function. You must initialize FitPars with something,
  93.        even if you have no idea about the result.
  94.        FitPars must be global, so that the model function can access the
  95.        parameters. With VD_nonlinfit, you can use just any functions
  96.        in your model.
  97.        For better comparison, we use the same polynomial approximation
  98.        as before, but now we code it as if we didn't know that a
  99.        polynomial actually is linear in its coefficients (and as if
  100.        we didn't know either how to efficiently code a polynomial at all).  */
  101.     double xi;
  102.     for( ui i=0; i<size; i++ )
  103.     {
  104.         xi  = X[i];
  105.         Y[i]= FitPars[0]
  106.             + FitPars[1] * xi
  107.             + FitPars[2] * xi * xi
  108.             + FitPars[3] * xi * xi * xi
  109.             + FitPars[4] * xi * xi * xi * xi
  110.             + FitPars[5] * xi * xi * xi * xi * xi;
  111.    }
  112. }
  113.  
  114.    // the following function is used with VD_multiNonlinfit:
  115. static void VSineModel( dVector Y, dVector X, ui size, unsigned nExperiment )
  116. {   /* According to the parameter nExperiment, the model function must
  117.        choose the correct parameters for the calculation of the model
  118.        function. The model function itself is the same for all experiments. */
  119.     double omega, phase, amp;
  120.     switch( nExperiment )
  121.     {
  122.         case 0: phase = FitPars[1];
  123.                 amp   = FitPars[4];
  124.                 break;
  125.         case 1: phase = FitPars[2];
  126.                 amp   = FitPars[5];
  127.                 break;
  128.         case 2: phase = FitPars[3];
  129.                 amp   = FitPars[6];
  130.     }
  131.     omega = FitPars[0];  // we assume this parameter to be the same
  132.     VDx_sin( Y, X, size, omega, phase, amp );
  133. }
  134.  
  135. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  136.                    LPSTR /* lpCmdLine */, int /* nCmdShow */)
  137. {
  138.     MSG       msg;                      /* MSG structure to pass to windows proc */
  139.     WNDCLASS  wc;
  140.     char      *AppName;
  141.  
  142.     AppName = "FitDemo";
  143.     if(!hPrevInstance)
  144.     {
  145.         wc.style      = CS_HREDRAW | CS_VREDRAW;
  146.         wc.lpfnWndProc= MainMessageHandler;
  147.         wc.cbClsExtra = 0;
  148.         wc.cbWndExtra = 0;
  149.         wc.hInstance  = hInstance;
  150.         wc.hIcon      = LoadIcon (hInstance, AppName);
  151.         wc.hCursor    = LoadCursor (NULL, IDC_ARROW);
  152.         wc.hbrBackground  = (HBRUSH) GetStockObject (WHITE_BRUSH);
  153.         wc.lpszMenuName   = AppName;
  154.         wc.lpszClassName  = AppName;
  155.         RegisterClass (&wc);
  156.     }
  157.                              /* create application's Main window:  */
  158.     hWndMain = CreateWindow (AppName,
  159.                              "OptiVec Data-Fitting Demo",
  160.                              WS_OVERLAPPEDWINDOW,
  161.                              CW_USEDEFAULT,     /* Use default X, Y, and width  */
  162.                              CW_USEDEFAULT,
  163.                              CW_USEDEFAULT,
  164.                              CW_USEDEFAULT,
  165.                              NULL,              /* Parent window's handle      */
  166.                              NULL,              /* Default to Class Menu       */
  167.                              hInstance,         /* Instance of window          */
  168.                              NULL);             /* Create struct for WM_CREATE */
  169.  
  170.  
  171.     if (hWndMain == NULL)
  172.     {
  173.         MessageBox(NULL, "Could not create window in WinMain", NULL, MB_ICONEXCLAMATION);
  174.         return (1);
  175.     }
  176.  
  177.     ShowWindow(hWndMain, SW_SHOWMAXIMIZED);     /* Display main window      */
  178.     UpdateWindow(hWndMain);
  179.  
  180.     while(GetMessage(&msg, NULL, 0, 0)) /* Main message loop */
  181.     {
  182.         TranslateMessage(&msg);
  183.         DispatchMessage(&msg);
  184.     }
  185.  
  186.     UnregisterClass (AppName, hInstance);
  187.     return (msg.wParam);
  188. }
  189.  
  190. LONG FAR PASCAL MainMe